package simplechat.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;

/**
 * @author Huhailong
 * @Description
 * @Date 2021/4/12.
 */
public class MainFrame extends JFrame {

    JPanel mainPanel;
    int WINDOW_WIDTH = 800;
    int WINDOW_HEIGHT = 500;

    public MainFrame(){
        addComponent();
        getContentPane().add(mainPanel);
        initializer();
    }

    private void initializer(){
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setTitle("simple chat");
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void addComponent(){
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.lightGray);
        leftPanel.setLayout(new BorderLayout());
        JLabel labelUserTitle = new JLabel("当前在线用户");
        ButtonGroup userList = new ButtonGroup();
        GridLayout gridLayout = new GridLayout();
        gridLayout.setColumns(1);
        int count = 0;
        List<JRadioButton> radioButtonList = new ArrayList<>();
        for(int i=0; i<100; i++){
            JRadioButton jrb = new JRadioButton("user"+i);
            userList.add(jrb);
            radioButtonList.add(jrb);
            count++;
        }
        gridLayout.setRows(count);
        JPanel userListPanel = new JPanel();
        JScrollPane jspUserList = new JScrollPane();
        userListPanel.setLayout(gridLayout);
        for(JRadioButton jrb:radioButtonList){
            jrb.addActionListener(new RadioListener());
            userListPanel.add(jrb);
        }
        jspUserList.add(userListPanel);
        jspUserList.setViewportView(userListPanel);
        labelUserTitle.setFont(new Font("黑体", Font.BOLD,20));
        leftPanel.add(labelUserTitle,BorderLayout.NORTH);
        leftPanel.add(jspUserList,BorderLayout.CENTER);
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BorderLayout());
        JTextArea showArea = new JTextArea();
        showArea.setRows(20);
        JScrollPane jspShow = new JScrollPane(showArea);
        JButton sendBtn = new JButton("发送");

        showArea.setEditable(false);
        JTextArea sendArea = new JTextArea();
        JScrollPane jspSend = new JScrollPane(sendArea);
        sendBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(sendArea.getText());
            }
        });
        rightPanel.add(jspShow,BorderLayout.NORTH);
        rightPanel.add(jspSend,BorderLayout.CENTER);
        rightPanel.add(sendBtn,BorderLayout.SOUTH);
        mainPanel.add(leftPanel,BorderLayout.WEST);
        mainPanel.add(rightPanel,BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        new MainFrame();
    }

}
